package club.yunqiang.service.impl;

import club.yunqiang.dto.ModifyDBInstanceConnection;
import club.yunqiang.dto.ModifyDBInstanceNetworkExpireTime;
import club.yunqiang.properties.RdsProperties;
import club.yunqiang.service.RdsApiService;
import club.yunqiang.util.HttpClientUtil;
import club.yunqiang.util.RdsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author YunQiang
 */
@Service
public class RdsApiServiceImpl implements RdsApiService{

	@Autowired
	private RdsProperties rdsProperties;

	@Override
	public Object allocateInstancePublicConnection(String connectionStringPrefix) {
		Map<String, String> param = new HashMap<>();
		param.put("Action", "AllocateInstancePublicConnection");
		param.put("connectionStringPrefix", connectionStringPrefix);

		String requestUrl = RdsUtils.generateRequestUrl(rdsProperties.getAccessKeyId(), rdsProperties.getSecret(), param);
		return HttpClientUtil.sendHttpGet(requestUrl);
	}

	@Override
	public Object describeDBInstanceNetInfo() {
		Map<String, String> param = new HashMap<>();
		param.put("Action", "DescribeDBInstanceNetInfo");
		param.put("DBInstanceId", rdsProperties.getDbInstanceId());

		String requestUrl = RdsUtils.generateRequestUrl(rdsProperties.getAccessKeyId(), rdsProperties.getSecret(), param);
		String result = "";
		try {
			result = HttpClientUtil.sendHttpGet(requestUrl);
		}catch (Exception e){
			e.printStackTrace();
			throw new RuntimeException(e.getMessage(),e);
		}
		return result;
	}

	@Override
	public Object modifyDBInstanceConnectionString(ModifyDBInstanceConnection modifyDBInstanceConnection) {
		Map<String, String> param = new HashMap<>();
		param.put("Action", "ModifyDBInstanceConnectionString");
		param.put("DBInstanceId", rdsProperties.getDbInstanceId());
		param.put("ConnectionStringPrefix", modifyDBInstanceConnection.getConnectionStringPrefix());
		param.put("CurrentConnectionString", modifyDBInstanceConnection.getCurrentConnectionString());
		param.put("Port", modifyDBInstanceConnection.getPort());

		String requestUrl = RdsUtils.generateRequestUrl(rdsProperties.getAccessKeyId(), rdsProperties.getSecret(), param);
		String result = "";
		try {
			result = HttpClientUtil.sendHttpGet(requestUrl);
		}catch (Exception e){
			e.printStackTrace();
			throw new RuntimeException(e.getMessage(),e);
		}
		return result;
	}

	@Override
	public Object modifyDBInstanceNetworkExpireTime(ModifyDBInstanceNetworkExpireTime modifyDBInstanceNetworkExpireTime) {
		Map<String, String> param = new HashMap<>();
		param.put("Action", "DescribeDBInstanceNetInfo");
		param.put("DBInstanceId", rdsProperties.getDbInstanceId());
		param.put("ConnectionString", modifyDBInstanceNetworkExpireTime.getConnectionString());
		param.put("ClassicExpiredDays", modifyDBInstanceNetworkExpireTime.getClassicExpiredDays() + "");

		String requestUrl = RdsUtils.generateRequestUrl(rdsProperties.getAccessKeyId(), rdsProperties.getSecret(), param);
		String result = "";
		try {
			result = HttpClientUtil.sendHttpGet(requestUrl);
		}catch (Exception e){
			e.printStackTrace();
			throw new RuntimeException(e.getMessage(),e);
		}
		return result;
	}

	@Override
	public Object modifyDBInstanceNetworkType(String instanceNetworkType) {
		Map<String, String> param = new HashMap<>();
		param.put("Action", "ModifyDBInstanceNetworkType");
		param.put("DBInstanceId", rdsProperties.getDbInstanceId());
		param.put("InstanceNetworkType", instanceNetworkType);

		String requestUrl = RdsUtils.generateRequestUrl(rdsProperties.getAccessKeyId(), rdsProperties.getSecret(), param);
		String result = "";
		try {
			result = HttpClientUtil.sendHttpGet(requestUrl);
		}catch (Exception e){
			e.printStackTrace();
			throw new RuntimeException(e.getMessage(),e);
		}
		return result;
	}

	@Override
	public Object releaseInstancePublicConnection(String currentConnectionString) {
		Map<String, String> param = new HashMap<>();
		param.put("Action", "ReleaseInstancePublicConnection");
		param.put("DBInstanceId", rdsProperties.getDbInstanceId());
		param.put("CurrentConnectionString", currentConnectionString);

		String requestUrl = RdsUtils.generateRequestUrl(rdsProperties.getAccessKeyId(), rdsProperties.getSecret(), param);
		String result = "";
		try {
			result = HttpClientUtil.sendHttpGet(requestUrl);
		}catch (Exception e){
			e.printStackTrace();
			throw new RuntimeException(e.getMessage(),e);
		}
		return result;
	}

	@Override
	public Object switchDBInstanceNetType(String connectionStringPrefix) {
		Map<String, String> param = new HashMap<>();
		param.put("Action", "SwitchDBInstanceNetType");
		param.put("DBInstanceId", rdsProperties.getDbInstanceId());
		param.put("ConnectionStringPrefix", connectionStringPrefix);

		String requestUrl = RdsUtils.generateRequestUrl(rdsProperties.getAccessKeyId(), rdsProperties.getSecret(), param);
		String result = "";
		try {
			result = HttpClientUtil.sendHttpGet(requestUrl);
		}catch (Exception e){
			e.printStackTrace();
			throw new RuntimeException(e.getMessage(),e);
		}
		return result;
	}
}
